package com.foredu.fiqs.web.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foredu.fiqs.domain.Department;
import com.foredu.fiqs.domain.EmpPost;
import com.foredu.fiqs.domain.EmpTitle;
import com.foredu.fiqs.domain.EmpType;
import com.foredu.fiqs.domain.Employee;
import com.foredu.fiqs.domain.Role;
import com.foredu.fiqs.service.IManagementService;
import com.foredu.fiqs.web.actionform.EmployeeActionForm;

public class EmployeeAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Employee> employees = managementService.findEmployee();
		request.setAttribute("employees", employees);
		return mapping.findForward("list");
	}
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<EmpPost> empPosts = managementService.findEmpPost();
		List<EmpTitle> empTitles = managementService.findEmpTitle();
		List<EmpType> empTypes = managementService.findempType();
		List<Department> departments = managementService.findDepartments();
		List<Role> roles = managementService.findRoles();
		request.setAttribute("empPosts", empPosts);
		request.setAttribute("empTitles", empTitles);
		request.setAttribute("empTypes", empTypes);
		request.setAttribute("departments", departments);
		request.setAttribute("roles", roles);
		return mapping.findForward("add");
	}
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Employee employee = new Employee();
		EmployeeActionForm eaf = (EmployeeActionForm) form;
		employee.setEmpId(eaf.getEmpId());
		employee.setEmpPost(managementService.findEmpPostById(eaf.getEmpPostId()));
		employee.setEmpTitle(managementService.findEmpTitleById(eaf.getEmpTitleId()));
		employee.setEmpType(managementService.findEmpTypeById(eaf.getEmpTypeId()));
		employee.setDepartment(managementService.findDepartmentById(eaf.getDepId()));
		employee.setStatus(eaf.getStatus());
		employee.setEmpDay(eaf.getEmpDay());
		employee.setIdCard(eaf.getIdCard());
		employee.setBankCard(eaf.getBankCard());
		employee.setUsername(eaf.getEmpId());
		employee.setNickname(eaf.getNickName());
		employee.setPassword(eaf.getPassword());
		if (null != eaf.getRoleIds()) {
			Set<Role> roles = new HashSet<Role>();
			String[] roleIds = eaf.getRoleIds();
			for (int i = 0; i < roleIds.length; i++) {
				Role role = managementService.findRoleById(Integer
						.parseInt(roleIds[i]));
				roles.add(role);
			}
			employee.setRoles(roles);
		} else {
			employee.setRoles(null);
		}
		managementService.saveEmployee(employee);
		return mapping.findForward("save");
	}
}
